$(function(){
    //SetInputValue();
    //获取url参数
	function GetQueryString(name){
		var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if(r!=null)return unescape(r[2]); return null;
    }
    
    //搜索
    searchInstructor=function(){
        var status=$("#status_select").val();
        var major=$("#major_select").val();
        var grade=$("#grade_select").val();
        var search=$("#search").val();
        var page=GetQueryString('page');
        var pageSize=GetQueryString('pageSize');
        url="./instructor-major-allocating.php?status="+status+"&major="+major+"&grade="+grade+"&search="+search;
        window.location.href=url;
    }

    $("select").change(function(){
        var status=GetQueryString('status');
        status=status==null?0:status;

        var major=GetQueryString('major');
        major=major==null?0:major;

        var grade=GetQueryString('grade');
        grade=grade==null?0:grade;

        var search=GetQueryString('search');
        search=search==null?'':search;


		var id=$(this).attr("id");
        switch(id){
			case 'instructor_page':
                url="./instructor-major-allocating.php?status="+status+"&major="+major
                +"&grade="+grade+"&page="+$(this).val()+"&search="+search;
				window.location.href=url;
			break;
			default:break;
		}
	});

    //编辑
    instructor_major_edit=function(table,id){
        uid=$(table).parents("tr").find(".uid").html();
        name=$(table).parents("tr").find(".name").html();
        grade=$(table).parents("tr").find(".grade").html();
        major=$(table).parents("tr").find(".major_id").html();

        if(grade!=='' && major!==''){
            $("#edit-uid").html(uid);
            $("#edit-name").html(name);
            $("#edit_grade_select").val(grade);
            $("#edit_major_select").val(major);
            $("#edit_checkbox").val(id);
            $("#modal-edit-instructor").modal("show");
        }else{
            layer.msg('该辅导员未分配年级和专业');
        }
        
    }
    //添加
    instructor_major_add=function(table,uid){
        uid=$(table).parents("tr").find(".uid").html();
        name=$(table).parents("tr").find(".name").html();
        grade=$(table).parents("tr").find(".grade").html();
        major=$(table).parents("tr").find(".major").html();

        $("#add-uid").html(uid);
        $("#add-name").html(name);
        $("#modal-add-instructor").modal("show");
    }

    //添加
    addInstructorMajor=function(){
        instructorInfo=new Object();
        instructorInfo.uid=$("#add-uid").html();
        instructorInfo.major=$("#add_major_select").val();
        instructorInfo.grade=$("#add_grade_select").val();
        
        //instructors=new Array();
        //instructors[0]=instructorInfo;
        instanceofJSON=JSON.stringify(instructorInfo);
        $.post("./php/instructor-major-allocating.php",{action:1,data:instanceofJSON},function(result){
            resultObject=JSON.parse(result);
            switch(resultObject.code){
                case '2001':
                    layer.msg(resultObject.message,{time:2000,btn:['确定'],icon:1},function(){
                        location.replace(location.href);
                    });
                    break;
                case '2002':
                    layer.msg(resultObject.message);
                    break;
                case '2003':
                    layer.msg(resultObject.message);
                    break;
                case '2004':
                    layer.msg(resultObject.message);
                    break;
            }
        });
    }

    //编辑
    editInstructorMajor=function(){
        instructorInfo=new Object();
        instructorInfo.id=$("#edit_checkbox").val();
        instructorInfo.major=$("#edit_major_select").val();
        instructorInfo.grade=$("#edit_grade_select").val();

        instanceofJSON=JSON.stringify(instructorInfo);
        $.post("./php/instructor-major-allocating.php",{action:3,data:instanceofJSON},function(result){
            resultObject=JSON.parse(result);
            switch(resultObject.code){
                case '2001':
                    layer.msg(resultObject.message,{time:3000,btn:['确定'],icon:1},function(){
                        location.replace(location.href);
                    });
                    break;
                case '2004':
                    layer.msg(resultObject.message);
                    break;
            }
        });

    }

    //删除
    deleteInstructorMajor=function(table,id){
        grade=$(table).parents("tr").find(".grade").html();
        major=$(table).parents("tr").find(".major").html();
        if(grade!=''&&major!=''){
            instructorInfo=new Object();
            instructorInfo.id=id;
            //instructorInfo.grade
            instructorInfoArray=new Array();
            instructorInfoArray[0]=instructorInfo;
            instructorJSON=JSON.stringify(instructorInfoArray);
            //alert(instructorJSON);
            layer.confirm('确定移除？',function(index){
                $.post("./php/instructor-major-allocating.php",{action:2,data:instructorJSON},function(result){
                    resultObject=JSON.parse(result);
                    switch(resultObject.code){
                        case '2001':
                            layer.msg(resultObject.message,{icon:1,time:2000,btn:['确定']},function(){
                                location.replace(location.href);
                            });
                        break;
                        case '2004':
                            layer.msg(resultObject.message,{icon:1,time:2000});
                        break;
                        default:break;
                    }
                });
            });
        }else{
            layer.msg("未分配，无需删除");
        }
    }

    //删除所选
    deleteInstructorMajorCheckbox=function(){
        instructorInfoArray=new Array();
        count=0;
        $(":checkbox[name='instructor_checkbox']:checked").each(function (){
            //alert($(this).prop("checked"));
            if($(this).val()!=''){
                instructorInfo=new Object();
                instructorInfo.id=$(this).val();
                instructorInfoArray[count]=instructorInfo;
                count++;
            }else{
                $(this).prop("checked",false);
            }
			
        });
        
        if(instructorInfoArray.length>0){
            layer.confirm('确定移除？',function(index){
                instructorJSON=JSON.stringify(instructorInfoArray);
                $.post("./php/instructor-major-allocating.php",{action:2,data:instructorJSON},function(result){
                    resultObject=JSON.parse(result);
                    switch(resultObject.code){
                        case '2001':
                            //$.Huimodalalert(resultObject.message,2000);
                            layer.msg(resultObject.message,{icon:1,time:3000},function(){
                                location.replace(location.href);
                            });
                            
                            //deleteSelectedTableLine();
                        break;
                        case '2004':
                            layer.msg(resultObject.message,{icon:2,time:1000});
                        break;
                        default:break;
                    }
                });
            });
        }else{
            layer.msg('未选中任何项');
        }
        
    }

    //删除选中的表格的行
	function deleteSelectedTableLine(){
		$(':checkbox[name=instructor_checkbox]:checked').each(function(index,e){  
			$(this).parents("tr").remove();
		});
	}

});